package com.farm.monitors.service.impl;

import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import com.farm.common.utils.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import com.farm.monitors.domain.Monitor;
import com.farm.monitors.mapper.MonitorsMapper;
import com.farm.monitors.domain.Monitors;
import com.farm.monitors.service.IMonitorsService;

/**
 * 监测管理Service业务层处理
 * 
 * @author yd
 * @date 2024-03-17
 */
@Service
public class MonitorsServiceImpl implements IMonitorsService 
{
    @Autowired
    private MonitorsMapper monitorsMapper;

    /**
     * 查询监测管理
     * 
     * @param monitorsId 监测管理主键
     * @return 监测管理
     */
    @Override
    public Monitors selectMonitorsByMonitorsId(Long monitorsId)
    {
        return monitorsMapper.selectMonitorsByMonitorsId(monitorsId);
    }

    /**
     * 查询监测管理列表
     * 
     * @param monitors 监测管理
     * @return 监测管理
     */
    @Override
    public List<Monitors> selectMonitorsList(Monitors monitors)
    {
        return monitorsMapper.selectMonitorsList(monitors);
    }

    /**
     * 新增监测管理
     * 
     * @param monitors 监测管理
     * @return 结果
     */
    @Transactional
    @Override
    public int insertMonitors(Monitors monitors)
    {
        int rows = monitorsMapper.insertMonitors(monitors);
        insertMonitor(monitors);
        return rows;
    }

    /**
     * 修改监测管理
     * 
     * @param monitors 监测管理
     * @return 结果
     */
    @Transactional
    @Override
    public int updateMonitors(Monitors monitors)
    {
        monitorsMapper.deleteMonitorByMonitorsId(monitors.getMonitorsId());
        insertMonitor(monitors);
        return monitorsMapper.updateMonitors(monitors);
    }

    /**
     * 批量删除监测管理
     * 
     * @param monitorsIds 需要删除的监测管理主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteMonitorsByMonitorsIds(Long[] monitorsIds)
    {
        monitorsMapper.deleteMonitorByMonitorsIds(monitorsIds);
        return monitorsMapper.deleteMonitorsByMonitorsIds(monitorsIds);
    }

    /**
     * 删除监测管理信息
     * 
     * @param monitorsId 监测管理主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteMonitorsByMonitorsId(Long monitorsId)
    {
        monitorsMapper.deleteMonitorByMonitorsId(monitorsId);
        return monitorsMapper.deleteMonitorsByMonitorsId(monitorsId);
    }

    /**
     * 新增${subTable.functionName}信息
     * 
     * @param monitors 监测管理对象
     */
    public void insertMonitor(Monitors monitors)
    {
        List<Monitor> monitorList = monitors.getMonitorList();
        Long monitorsId = monitors.getMonitorsId();
        if (StringUtils.isNotNull(monitorList))
        {
            List<Monitor> list = new ArrayList<Monitor>();
            for (Monitor monitor : monitorList)
            {
                monitor.setMonitorsId(monitorsId);
                list.add(monitor);
            }
            if (list.size() > 0)
            {
                monitorsMapper.batchMonitor(list);
            }
        }
    }
}
